import headerWrap from "../commonComponents/header.vue"
import noDataWrap from "../commonComponents/noMoreData.vue"
import loadingWrap from "../commonComponents/loadingInPage.vue"
import switchTab from "../commonComponents/switchTab.vue"

export default {
  data(){
    return {
      show: true,
      switchFlag: 0,
      sortHeight: 0,
      titleFlag: "",
      tabData: ["在办事宜", "办结事宜"],
      pendingPageIndex: 0,
      completePageIndex: 0,
      pendingList: [],
      completeList: [],
      detailPath: null,
      icon: "",
      iconBc: "",
    }
  },
  methods: {
    init(){
      this.titleFlag = _methods.getUrlParam("title") - 0;
      this.icon = {
        4: "icon-jrkb",
        5: "icon-tsgl",
      }[this.titleFlag];
      this.iconBc = {
        4: "iy-bg-orange",
        5: "iy-bg-yellow",
      }[this.titleFlag];
      _methods.headerTitleChange(this.titleFlag);
      setTimeout(() => {
        this.pendingListInit();
        this.completeListInit();
      }, 100);
    },
    pendingListInit() {
      let self = this;
      _data.todayNewsPendingList = _methods.initMeScroll("pending", {
        //刷新
        down: {
          callback: () => {
            self.getList(1, 1);
          }
        },
        //加载更多
        up: {
          callback: () => {
            self.getList(1, 2);
          }
        }
      });
    },
    completeListInit() {
      _data.todayNewsCompleteList = _methods.initMeScroll("complete", {
        //刷新
        down: {
          callback: () => {
            this.getList(2, 1);
          }
        },
        //加载更多
        up: {
          callback: () => {
            this.getList(2, 2);
          }
        }
      });
    },
    //列表
    getList(listFlag, scrollFlag){
      /*
       listFlag：1.待办 2.已办 3.办结
       scrollFlag：1：刷新 2：加载更多
       */
      let self = this;
      //列表参数
      let listKey = {
        1: "pendingList",
        2: "completeList",
      }[listFlag];
      //scrollKey
      let scrollKey = {
        1: "todayNewsPendingList",
        2: "todayNewsCompleteList",
      }[listFlag];
      //页码参数
      let pageIndex = {
        1: "pendingPageIndex",
        2: "completePageIndex",
      }[listFlag];

      let ajUrl = {
        4: _ajaxUrls.todayBulletin,
        5:_ajaxUrls.todayBulletin,
      }[this.titleFlag];

      let status = {
        1: 0,
        2: 1,
      }[listFlag];
      let type = {
        4: 1,
        5: 2,
      }[this.titleFlag];

      let data = {
        size: window._data.pageSize,
        type,
        status,
      };

      //ajax获取列表数据
      let ajData = (s, pg = 1) => {
        data.page = pg;
        _ajax.noLoading(ajUrl.url, data, (v) => {
          let val = v.result.records;
          let len = val.length;
          let arr;
          if (s === 1) {
            //刷新
            arr = [];
          }
          else {
            arr = this[listKey];
          }

          val.forEach((i) => {
            i.isTodo = i.approvalUserID == window._data.userInfo.userId ? "i-state-pend" : "i-state-doing";
            arr.push(i);
          });

          self[listKey] = arr;

          _data[scrollKey].endSuccess(len, len == _data.pageSize);
        }, ajUrl.type);
      };
      if (scrollFlag === 1) {
        //刷新列表
        self[pageIndex] = 1;
        ajData(scrollFlag, self[pageIndex]);
      } else {
        // 加载更多
        self[pageIndex] = parseInt(self[pageIndex]) + 1;
        ajData(scrollFlag, self[pageIndex]);
      }
    },
    go_details(item){
      let title = this.titleFlag * 10;
      let path =
        `/todayBulletin/detail?id=${item.id}&title=${title}`;
      _methods.goPath(path);
    },
    switchClick(i){
      this.switchFlag = i;
    },
  },
  components: {
    headerWrap,
    noDataWrap,
    loadingWrap,
    switchTab,
  },
  mounted(){
    _methods.isNotPush().then(() => {
      this.init();
    });
  }
}
